(function () {
    function Portal() {

    }

    Portal.prototype = {
        Constructor: Portal
    };

    Portal.prototype.init = function () {
        this.showTime();
        this.queryOrderView();
        this.queryNewOrderView();
        this.queryNightBackUserView();
        this.queryRoomActView();
        this.queryNightBackUserView();
        this.queryRecentlyMsgView();
        this.queryRoomTypeView();
        this.queryEvaluateView();
        this.queryVipView();
        return this;
    };

    Portal.prototype.queryOrderView = function () {
        SysTool.Ajax.ajaxJsonCall('portal/order', {}, function (data) {
            var totalCnt = data.totalCnt || 0;
            var needConfirmCnt = data.needConfirmCnt || 0;
            var validCnt = data.validCnt || 0;
            var forNightCnt = data.forNightCnt || 0;
            $('#orderTotalCnt').html(totalCnt);
            $('#orderValidCnt').html(needConfirmCnt);
            $('#orderNeedAuthCnt').html(validCnt);
            $('#orderNightBackUserCnt').html(forNightCnt);
        });
    };

    Portal.prototype.queryNewOrderView = function () {
        SysTool.Ajax.ajaxJsonCall('order/page', {
            iDisplayStart: 0,
            iDisplayLength: 10,
            orderStatus: 8
        }, function (data) {
            var html = '';
            for (var i = 0; i < data.aaData.length; i++) {
                var d = data.aaData[i];
                var id = d.id;
                var orderNo = d.orderNo;
                var orderName = d.orderName;
                if (orderName && orderName.length > 20)
                    orderName = orderName.substr(0, 20) + '...';
                var createTime = d.createTime;
                html += '<a href="' + app_path + '/order/detail/' + id + '" class="col-xs-12">' +
                    '<span class="pull-left">' + '单号' + orderNo + '&emsp;&emsp;' + orderName + '</span>' +
                    '<span class="pull-right">' + SysTool.Date.formatDate(createTime, 'yyyy-MM-dd hh:mm:ss') + '</span>' +
                    '</a>';
            }
            $('#newOrder').html(html);
        });
    };

    Portal.prototype.queryNightBackUserView = function () {
        // TODO 查询夜归人概况
    };

    Portal.prototype.queryRoomActView = function () {
        SysTool.Ajax.ajaxJsonCall('portal/freePurchase', {}, function (data) {
            var status = data.status || '未知';
            var betweenTime = data.betweenTime || '';
            var totalRoomTypeCnt = data.totalRoomTypeCnt || 0;
            var hotelRoomTypeCnt = data.hotelRoomTypeCnt || 0;
            var hotelUsedRoomTypeCnt = data.hotelUsedRoomTypeCnt || 0;
            var hotelInvolveRoomCnt = data.hotelInvolveRoomCnt || 0;
            $('#roomActStatus').html('<span class="label label-info arrowed-in arrowed-in-right">' + status + '</span>');
            $('#roomActTime').html(betweenTime);
            $('#roomActTotalRoomCnt').html(totalRoomTypeCnt);
            $('#roomActHotelRoomCnt').html(hotelRoomTypeCnt);
            $('#roomActHotelUsedRoomCnt').html(hotelUsedRoomTypeCnt);
            $('#roomActInvolveRoomCnt').html(hotelInvolveRoomCnt);
        });
    };

    Portal.prototype.queryRoomTypeView = function () {
        SysTool.Ajax.ajaxJsonCall('portal/roomType', {}, function (data) {
            var totalCnt = data.totalCnt || 0;
            var onlineRoomTypeCnt = data.onlineRoomTypeCnt || 0;
            var offlineRoomTypeCnt = data.offlineRoomTypeCnt || 0;
            $('#roomTypeTotalCnt').html(totalCnt);
            $('#roomTypeOnlineCnt').html(onlineRoomTypeCnt);
            $('#roomTypeOfflineCnt').html(offlineRoomTypeCnt);
        });
    };

    Portal.prototype.queryRecentlyMsgView = function () {
        SysTool.Ajax.ajaxJsonCall('portal/recentlyMsg', {}, function (data) {
            var html = '';
            for (var i in data) {
                var d = data[i];
                var id = d.id;
                var content = d.content;
                if (content && content.length > 20)
                    content = content.substr(0, 20) + '...';
                var receiveTime = d.receiveTime;
                var statusCode = d.statusCode;
                var statusName = d.statusName;
                var notRead = statusCode == '01';
                html += '<a href="' + app_path + '/msg/detail/' + id + '" class="col-xs-12">' +
                    '<span class="pull-left">' + (notRead ? '<b>' : '') + content + (notRead ? '</b>' : '') + '</span>' +
                    '<span class="pull-right">' + receiveTime + '</span>' +
                    '</a>';
            }
            $('#recentlyMsg').html(html);
        });
    };

    Portal.prototype.queryEvaluateView = function () {
        SysTool.Ajax.ajaxJsonCall('portal/evaluate', {}, function (data) {
            var totalCnt = data.totalCnt || 0;
            var averageScore = parseFloat(data.averageScore) || '-';
            var todayEvaluateCnt = data.todayEvaluateCnt || 0;
            $('#evaludateTotalCnt').html(totalCnt);
            $('#evaludateAvgScore').html(averageScore);
            $('#evaludateTodayCnt').html(todayEvaluateCnt);
        });
    };

    Portal.prototype.queryVipView = function () {
        SysTool.Ajax.ajaxJsonCall('portal/vip', {}, function (data) {
            var totalCnt = data.totalCnt || 0;
            var needAuthCnt = data.needAuthCnt || 0;
            var newTotalCnt = data.newTotalCnt || 0;
            $('#vipTotalCnt').html(totalCnt);
            $('#vipNeedAuthCnt').html(needAuthCnt);
            $('#vipTodayCnt').html(newTotalCnt);
        });
    };

    Portal.prototype.showTime = function () {
        $('#show_time').text(SysTool.Date.formatDate(new Date(), 'yyyy年MM月dd日') + ' ' + SysTool.Date.getCurrentWeekInChinese());
    };
    var portal = new Portal().init();
})();